package br.com.findimoveis.application;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.findimoveis.constants.TipoImovel;
import br.com.findimoveis.domain.Anunciante;
import br.com.findimoveis.domain.Anuncio;
import br.com.findimoveis.domain.Imovel;

import com.google.appengine.api.datastore.AsyncDatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.labs.repackaged.com.google.common.collect.Lists;

/**
 * @author soliva
 *
 */
public class ImovelPesquisaController  extends BaseController {

	private static final long serialVersionUID = 3242812721133509155L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		AsyncDatastoreService asyncDatastoreService = DatastoreServiceFactory.getAsyncDatastoreService();


		Query query = new Query(Imovel.class.getSimpleName());

		PreparedQuery preparedQuery = asyncDatastoreService.prepare(query);
		List<Imovel> imoveis = 	Lists.newArrayList();
		for(Entity imov: preparedQuery.asList(FetchOptions.Builder.withDefaults())){
			imoveis.add(new Imovel().createNewImovel(new Anunciante(NOME_ANUNCIANTE, TELEFONE_ANUNCIANTE, EMAIL_ANUNCIANTE),
					new Anuncio(TITULO_ANUNCIO),((Double)imov.getProperty(VALOR)), ((Double)imov.getProperty(AREA_UTI)),
					((Long)imov.getProperty(DORMITORIO)).intValue(),((String)imov.getProperty(DESCRICAO)), ((TipoImovel)imov.getProperty(TIPO_IMOVEL))));
		}

		request.setAttribute("imoveis",imoveis);

		forward(request, response,"/list.jsp");
	}



}
